/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sessionBeans;

import entity.Users;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Minh Luc
 */
@Stateless
public class UsersFacade extends AbstractFacade<Users> implements UsersFacadeLocal {

    @PersistenceContext(unitName = "WebBasedClaimProcessing-ejbPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public UsersFacade() {
        super(Users.class);
    }

    @Override
    public Users checkLogin(String userName, String password) {
        String sQuery = "SELECT u FROM Users u WHERE u.userName = :userName AND u.password = :password";
        Query query = em.createQuery(sQuery);
        query.setParameter("userName", userName);
        query.setParameter("password", password);
        if (query.getResultList().size() == 1) {
            return (Users) query.getSingleResult();
        }
        return null;
    }

    @Override
    public Users hasAnswer(String Username, String Answer) {
        String sQuery = "SELECT u FROM Users u WHERE u.userName LIKE :userName AND u.answer LIKE :answer";
        Query q = em.createQuery(sQuery);
        q.setParameter("userName", Username);
        q.setParameter("answer", Answer);
        if (q.getResultList().size() == 1) {
            return (Users) q.getSingleResult();
        }
        return null;
    }

    @Override
    public Users findByCodeRegister(String RegisterCode) {
        Query q = em.createNamedQuery("Users.findByCodeRegister");
        q.setParameter("codeRegister", RegisterCode);
        if (q.getResultList().size() == 1 && ((Users) q.getSingleResult()).getUserName().length() == 36) {
            return (Users) q.getSingleResult();
        }
        return null;
    }

    public List<Users> findAllCodeNotRegister() {
        Query q = em.createNamedQuery("Users.findAll");
        List<Users> list = q.getResultList();
        List<Users> listNotRegister = new ArrayList<Users>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUserName().length() == 36) {
                listNotRegister.add(list.get(i));
            }
        }
        return listNotRegister;

    }

    @Override
    public Users hasEmail(String email) {
        Query query = em.createNamedQuery("Users.findByEmail");
        query.setParameter("email", email);
        if (query.getResultList().size() == 1) {
            return (Users) query.getSingleResult();
        }
        return null;
    }

    public List<entity.Users> findByRoleName(String roleName) {
        String sQuery = "SELECT u FROM Users u WHERE  u.roles.roleName = :roleName";
        Query query = em.createQuery(sQuery);
        query.setParameter("roleName", roleName);


        List<Users> list = query.getResultList();
        List<Users> listRegister = new ArrayList<Users>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUserName().length() != 36) {             
                listRegister.add(list.get(i));
            }
        }
        return listRegister;
    }
}
